﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Core.Data.Orders;
using Shop.Core.Domain.Orders;
using System.Collections;

namespace Shop.Data.IBatis.Orders
{
    public class OrderRepository : DaoBase, IOrderRepository
    {
        /// <summary>
        /// 获取订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Order GetById(string id)
        {
            return this.FindByIdList(new string[] { id }).FirstOrDefault();
        }

        /// <summary>
        /// 创建订单号
        /// </summary>
        /// <returns></returns>
        public string CreateOrderNumber()
        {
            Hashtable hash = new Hashtable(1);
            hash["OrderNumber"] = null;
            this.Writer.Insert(this.GetOrderSM("CreateOrderNumber"), hash);
            return hash["OrderNumber"] as string;
        }

        /// <summary>
        /// 插入订单号
        /// </summary>
        /// <param name="entity"></param>
        public bool Insert(Core.Domain.Orders.Order entity)
        {
            string orderNumber = this.CreateOrderNumber();
            entity.Id = orderNumber;
            foreach (OrderProduct op in entity.Products)
            {
                op.OrderNumber = orderNumber;
            }
            this.Writer.Insert(this.GetOrderSM("InsertOrder"), entity);
            foreach (OrderProduct op in entity.Products)
            {
                this.Writer.Insert(this.GetOrderSM("InsertOrderProduct"), op);
            }
            return true;
        }

        /// <summary>
        /// 没有实现
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(Core.Domain.Orders.Order entity)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 逻辑删除订单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(Core.Domain.Orders.Order entity)
        {
            return this.Writer.Update(this.GetOrderSM("DeleteOrder"), entity) == 1;
        }

        /// <summary>
        /// 批量查询订单
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public IList<Order> FindByIdList(IList<string> idList)
        {
            IList<Order> orders = this.Reader.QueryForList<Order>(this.GetOrderSM("SelectByIdList"), idList);
            if (orders == null || orders.Count == 0)
                return orders;
            IList<OrderProduct> orderProducts = this.Reader.QueryForList<OrderProduct>(this.GetOrderSM("SelectOrderProducts"), idList);
            if (orderProducts != null && orderProducts.Count > 0)
            {
                IDictionary<string, IList<OrderProduct>> dictProducs = new Dictionary<string, IList<OrderProduct>>();
                foreach (OrderProduct op in orderProducts)
                {
                    if (!dictProducs.ContainsKey(op.OrderNumber))
                    {
                        dictProducs[op.OrderNumber] = new List<OrderProduct>();
                    }
                    dictProducs[op.OrderNumber].Add(op);
                }
                foreach (Order o in orders)
                {
                    if (dictProducs.ContainsKey(o.Id))
                    {
                        o.Products = dictProducs[o.Id];
                    }
                }
            }
            return orders;
        }

        protected virtual string GetOrderSM(string id)
        {
            return string.Format("Shop.Order.{0}", id);
        }
    }
}
